import re message = input('what is your message?') expression = re.compile('^((\(\d{3}\))|(\d{3}))[- ]\d{3}-\d{4}$') if expression.match(message): print('it matches') else: print('it does NOT match')